package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解：
 *   1.组合注解,等价于 Controller注解 + ResponseBody注解;
 *   2.添加该注解后,所有的控制器方法上无需再添加 @ResponseBody注解 了;
 * Slf4j注解:
 *   1.Lombok提供的日志注解,添加在类上;
 *   2.在类上添加该注解后,会在该类中创建一个日志对象log,调用日志对象log的方法来打印各种级别的日志;
 *     log.trace("追踪信息日志");
 *     log.debug("调试信息日志"); <----->开发过程中使用
 *     log.info("运行信息日志");
 *     log.warn("警告信息日志");
 *     log.error("错误信息日志"); <----->开发过程中使用[全局异常处理器中处理异常时使用]
 * Knife4j中注解:
 *   1.@Tag(name=""):控制器类名在Knife4j文档中的显示名称;
 *   2.@Operation(summary = ""):控制器方法名称在Knife4j文档中的显示名称;
 *   3.@ApiOperationSupport(order = 10):控制器方法顺序,数字越小优先级越高;
 *   4.@Schema(description = "", required = true, example = ""):封装参数的描述信息;
 *   5.@Parameter(name="", description="", required = true, example = "", hidden = true)
 *     5.1 非封装单个参数描述信息;
 *     5.2 hidden=true表示该参数在Knife4j文档中不显示;
 *   6.@Parameters(value = {@Parameter(),@Parameter()}):非封装多个参数的描述信息;
 */

//@Controller
@Slf4j
@RestController
@Tag(name = "01-资讯管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    //自动装配
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 1.添加资讯功能
     */
    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "title", description = "资讯标题"),
            @Parameter(name = "content", description = "资讯内容"),
            @Parameter(name = "type", description = "资讯类型"),
            @Parameter(name = "status", description = "资讯状态"),
            @Parameter(name = "noticeAddParam", hidden = true)
    })
    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        log.debug("添加资讯: " + noticeAddParam);
        //终极目标: 将资讯信息插入到数据库的notice表中;
        // new Notice().var + 回车 + 回车;
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        //处理其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("准备插入数据库: " + notice);
        noticeMapper.insertNotice(notice);//调用接口方法

        return JsonResult.ok();
    }

    /**
     * 2.查询资讯列表功能
     */
    @GetMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listNotice(NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery = " + noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList(noticeListQuery);
        log.debug("noticeListVOS = " + noticeListVOS);

        return JsonResult.ok(noticeListVOS);
    }

    /**
     * 3.查询资讯详情功能
     */
    @GetMapping("detail")
    @Operation(summary = "资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {
            @Parameter(name = "id", description = "资讯ID", required = true, example = "1"),
            @Parameter(name = "username", description = "用户名", required = true, example = "admin", hidden = true)
    })
    public JsonResult detailNotice(long id, String username){
        //username参数仅仅是为了演示Knife4j的文档描述,没有其他作用
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeById(id);

        return JsonResult.ok(noticeDetailInfoVO);
    }

    /**
     * 4.删除资讯功能
     */
    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult delNotice(long id){
        log.debug("id = " + id);
        int i = noticeMapper.deleteNoticeById(id);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 5.修改资讯功能
     *   UPDATE notice SET title=?,content=?,type=?,status=?,update_time=? WHERE id=?
     */
    @PostMapping("update")
    @Operation(summary = "修改资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult updateNotice(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        //打印日志
        log.debug("notice = " + notice);
        int i = noticeMapper.updateNoticeById(notice);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}








